package com.itheima.comtroller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.utils.Result;
import com.itheima.service.EmpService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/7 23:48
 * @Desc:
 */
@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        boolean flag = empService.login(emp.getUsername(), emp.getPassword());
        if (flag) {
            //生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            Emp emp1 = empService.findEmpByUsername(emp.getUsername()).get(0);
            claims.put("id", emp1.getId());
            claims.put("name", emp1.getName());
            claims.put("username", emp1.getUsername());
            String jwt = Jwts.builder()
                    .setClaims(claims)
                    .signWith(SignatureAlgorithm.HS256, "itheima")
                    .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 30))
                    .compact();
            //将jwt令牌信息响应给浏览器
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }

    //添加员工
//    @RequestMapping(value = "/emps", method = RequestMethod.POST)
    @PostMapping("/emps")
    public Result addEmp(@RequestBody Emp emp) {
        boolean flag = empService.addEmp(emp);

        return flag == true ? Result.success() : Result.error("前三项未填写完整或用户名已存在");
    }

    //批量删除员工信息
//    @RequestMapping(value = "/emps/{ids}", method = RequestMethod.DELETE)
    @DeleteMapping(value = "/emps/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids) {
        empService.deleteEmp(ids);

        return Result.success();
    }

    //修改员工信息
//    @RequestMapping(value = "/emps", method = RequestMethod.PUT)
    @PutMapping(value = "/emps")
    public Result updateEmp(@RequestBody Emp emp) {
        boolean flag = empService.updateEmp(emp);

        return flag == true ? Result.success() : Result.error("用户名已存在");
    }

    //查询员工信息
//    @RequestMapping(value = "/emps", method = RequestMethod.GET)
    @GetMapping(value = "/emps")
    public Result empList(String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                          @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")
                          Integer pageSize) {
        PageBean pageBean = empService.empList(name, gender, begin, end, page, pageSize);

        return pageBean.getRows().size() != 0 ? Result.success(pageBean) : Result.error("未查询到任何信息");
    }

    //根据id查询员工信息
//    @RequestMapping(value = "/emps/{id}", method = RequestMethod.GET)
    @GetMapping(value = "/emps/{id}")
    public Result findEmpById(@PathVariable Integer id) {
        List<Emp> emps = empService.findEmpById(id);

        return emps.size() != 0 ? Result.success(emps.get(0)) : Result.error("未查询到任何信息");
    }

    //文件上传
//    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @PostMapping(value = "/upload")
    public Result fileUpload(MultipartFile image) throws Exception {
        String url = empService.fileUpload(image);

        return Result.success(url);
    }
}
